package net.core.service.encrype;

public class Encrypter {

	/**
	 * 对字符串进行加密和解密
	 * 
	 * @param String
	 *            str 原字符串
	 * @param boolean bEncry :true加密 ,false解密
	 * @return String
	 */
	private static String Encrypt(String str, boolean bEncry) {
		String result = "";
		try {
			byte[] strByte = str.getBytes();
			if (bEncry) {
				for (int i = 0; i < str.length(); i++) {
					if (strByte[i] <= 50)
						result = result + (char) (strByte[i] - 10);
					else if (strByte[i] > 50 && strByte[i] <= 90)
						result = result + (char) (strByte[i] + 30);
					else if (strByte[i] > 90 && strByte[i] <= 128)
						result = result + (char) (strByte[i] - 50);
					else if (strByte[i] > 128)
						result = result + (char) (strByte[i] - 6);
				}
			} else {
				for (int i = 0; i < str.length(); i++) {
					if (strByte[i] <= 40)
						result = result + (char) (strByte[i] + 10);
					else if (strByte[i] > 40 && strByte[i] <= 80)
						result = result + (char) (strByte[i] + 50);
					else if (strByte[i] > 80 && strByte[i] <= 120)
						result = result + (char) (strByte[i] - 30);
					else if (strByte[i] > 120)
						result = result + (char) (strByte[i] + 6);
				}
			}
		} catch (Exception e) {
			System.err.println("操作出错原因=" + e.toString());
			result = "-1";
		}
		return result;
	}

	/**
	 * 字符串加密
	 * 
	 * @param pw
	 *            需要加密的字符串
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-26 下午02:05:29
	 */
	public static String encrypt(String str) {
		try {
			return Encrypt(str, true);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 字符串解密
	 * 
	 * @param pw
	 *            需要解密的字符串
	 * @return
	 * @description
	 * @version 1.0
	 * @author xiaowei
	 * @update 2012-7-26 下午02:05:50
	 */
	public static String decrypt(String str) {
		try {
			return Encrypt(str, false);
		} catch (Exception e) {
		}
		return null;
	}

}
